/*******************************************************************************
Author: Jason Robey
Date: 5/15/2025
Purpose: Run  APCC models for five-year five-age analyses
Output: Table 2 descriptives, Table 4 model results, and Tabel A5 model results. 
*******************************************************************************/

cls 
clear 
capture log close

*Define using working directories
global path ""

*raw data directory
global rd "$path/"

*working data directory
global wd "$path/"

*log file directory
global ld "$path/"

*output file directory
global od "$path/"

cd $wd

*Data string to append to log file
local date: display %td_CCYY-NN-DD date(c(current_date), "DMY")
local date_string = subinstr(trim("`date'"), "-" , ".", .)

*Starting log file 
log using "$ld/name.`date_string'.log", replace

ssc install estout

/*******************************************************************************
Analysis of Categorical Age-Year-Cohort Data
*******************************************************************************/

use 2.1.1.merge_yearcat.dta, clear

*Case selection to remain consistent with prior literature 
keep if year_cat<2015

*Only keeping age-cohort combinations with valid data for all three cohort homicide measures
keep if !missing(ahar_pc, ahar_pc_lead, ahar_rc)


*Descriptive Stats for Table 2
table (var) if !missing(ahar_pc, anmar, apop_per_15_19), ///
	stat(mean ahar_pc ahar_pc_lead ahar_rc anmar wnmar bnmar apop_per_15_19) ///
	stat(sd ahar_pc ahar_pc_lead ahar_rc anmar wnmar bnmar apop_per_15_19) ///
	stat(count ahar_pc ahar_pc_lead ahar_rc anmar wnmar bnmar apop_per_15_19) ///
	stat(freq) ///
	nformat(%9.1fc percent mean sd) ///
	sformat("%s%%" percent) ///
	sformat("(%s)" sd)	
collect export "$od/table2a.docx", replace	


***APCC Regression Modeling 
est clear 

***Table 4 
foreach dem in a w b {
	foreach var in har {
		foreach typ in pc pc_lead rc {
			quietly reg `dem'`var'_`typ'_log i.year_cat i.age_cat apop_per_15_19 `dem'nmar, robust 
			est sto `dem'`var'`typ'
		}
	}
}


*All Models 
#delimit ;
esttab *har*,
	nonumber label obslast constant omit
	title("National APCC Models for Logged Homicide Arrest Rates")
	b(%10.3f) se(%10.3f) ar2(%9.3f) star(* 0.05 ** 0.01 *** 0.001);
#delimit cr

*All Models Exported in CSV
#delimit ;
esttab *har* using "$od/Table4.csv", plain replace
	nonumber label obslast constant omit
	title("National APCC Models for Logged Homicide Arrest Rates")
	b(%10.3f) se(%10.3f) ar2(%9.3f) star(* 0.05 ** 0.01 *** 0.001);
#delimit cr


***Appendix Table A5 - models with more alternative cohort homicide measures 
foreach dem in a w b {
	foreach var in har {
		foreach typ in pc pc_lead rc rca1 rca2 pc_both pc_my {
			quietly reg `dem'`var'_`typ'_log i.year_cat i.age_cat apop_per_15_19 `dem'nmar, robust 
			est sto `dem'`var'`typ'
		}

	}
}



*Total Population Models
#delimit ;
esttab ahar*,
	nonumber label obslast constant omit
	title("National APCC Models for Logged Homicide Arrest Rates")
	b(%10.3f) se(%10.3f) ar2(%9.3f) star(* 0.05 ** 0.01 *** 0.001);
#delimit cr

*White Population Models
#delimit ;
esttab whar*,
	nonumber label obslast constant omit
	title("National APCC Models for Logged Homicide Arrest Rates")
	b(%10.3f) se(%10.3f) ar2(%9.3f) star(* 0.05 ** 0.01 *** 0.001);
#delimit cr

*Black Population Models
#delimit ;
esttab bhar*,
	nonumber label obslast constant omit
	title("National APCC Models for Logged Homicide Arrest Rates")
	b(%10.3f) se(%10.3f) ar2(%9.3f) star(* 0.05 ** 0.01 *** 0.001);
#delimit cr

*All Models Exported in CSV
#delimit ;
esttab *har* using "$od/AppTableA5.csv", plain replace
	nonumber label obslast constant omit
	title("National APCC Models for Logged Homicide Arrest Rates")
	b(%10.3f) se(%10.3f) ar2(%9.3f) star(* 0.05 ** 0.01 *** 0.001);
#delimit cr


*Footnote 19 calculation - modeling without robust standard errors for SUE tests 
est clear 

sort age_cat year_cat 
list year_cat age_cat ahar_pc ahar_pc_lead ahar_pc_both ahar_rc if anmar<. & apop_per_15_19<.
sum ahar_pc_log if anmar<. & apop_per_15_19<. 


foreach dem in a w b {
	foreach var in har {
		foreach typ in pc pc_lead rc {
			quietly reg `dem'`var'_`typ'_log i.year_cat i.age_cat apop_per_15_19 `dem'nmar ///
			if !missing(ahar_pc, anmar, apop_per_15_19)
			est sto `dem'`var'`typ'
		}

	}
}

hausman bharpc bharpc_lead

suest bharpc bharpc_lead, vce(robust)
test [bharpc_mean]apop_per_15_19 = [bharpc_lead_mean]apop_per_15_19
test [bharpc_mean = bharpc_lead_mean]:apop_per_15_19

log close 
